﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Entity;
using eShop.Utility;
using System.Text;
using ECommerce.Library.Web;
using eShop.Business;
using eShop.Model;
using eShop.DataAccess;
using eShop.DataAccess.Converter;

namespace eShop.Business
{
    public class UserManager
    {
        public static bool UpdateCustomerUser(User user)
        {
            DataAccess.Entity.UserProfile eUserProfile = UserProfileConverter.ConvertToEntity(user.Profile);
            return UserProfileDAL.Update(eUserProfile);
        }
        public static void CreateNewCustomerUser(User user)
        {
            if (IsUserExists(user.UserName))
            {
                throw new Exception("Username already exist.");
            }
            user.Role = UserRoleType.Customer;
            DataAccess.Entity.User eUser = UserConverter.ConvertToEntity(user);
            if (UserDAL.Insert(eUser))
            {
                user.Id = eUser.Id;
                user.Profile.UserId = user.Id;
                DataAccess.Entity.UserProfile eUserProfile = UserProfileConverter.ConvertToEntity(user.Profile);
                if (UserProfileDAL.Insert(eUserProfile))
                {
                    user.Profile.Id = eUserProfile.Id;
                }
                else
                {
                    UserDAL.DeleteByKeys(user.Id);
                    throw new Exception("Failed to create user profile.");
                }
            }
            else
            {
                throw new Exception("Failed to create user.");
            }
        }
        public static User GetUserByUserName(string userName)
        {
            List<User> users = UserConverter.ConvertToModelObject(UserDAL.GetUserByUserName(userName.Trim()));
            if (users.Count > 0)
            {
                users[0].Profile = GetUserProfileByUserId(users[0].Id);
                return users[0];
            }
            return null;
        }

        public static bool IsUserExists(string userName)
        {
            return GetUserByUserName(userName) != null;
        }

        public static bool IsLoginValid(string userName, string password)
        {
            User user = GetUserByUserName(userName);
            return (user != null && user.Password == password);
        }

        public static bool Login(string userName, string password, UserLocationType userLocation)
        {
            if (IsLoginValid(userName, password))
            {
                SessionManager.LoggedUser = SessionManager.LoggedAdminUser = null;
                User loggedInUser = GetUserByUserName(userName);
                if (loggedInUser.Role == UserRoleType.Admin)
                {
                    SessionManager.LoggedAdminUser = loggedInUser;
                }
                else
                {
                    SessionManager.LoggedUser = loggedInUser;
                }
                if (SessionManager.LoggedUser != null)
                {
                    SessionManager.LoggedUser.Profile = GetUserProfileByUserId(SessionManager.LoggedUser.Id);
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool Logout(UserLocationType userLocation)
        {
            if (userLocation == UserLocationType.FrontEnd)
            {
                SessionManager.LoggedUser = null;
            }
            else
            {
                SessionManager.LoggedAdminUser = null;
            }
            if (CookieManager.IsCookieExists(CookieManager.USER_NAME))
            {
                CookieManager.RemoveCookie(CookieManager.USER_NAME);
                CookieManager.RemoveCookie(CookieManager.PASSWORD);
            }
            return true;
        }

        public static bool UpdatePassword(int userId, string password)
        {
            List<User> users = UserConverter.ConvertToModelObject(UserDAL.GetByKeys(userId));
            if (users.Count > 0)
            {
                users[0].Password = password;
                return UserDAL.Update(UserConverter.ConvertToEntity(users[0]));
            }
            return false;
        }

        public static UserProfile GetUserProfileByUserId(int userId)
        {
            List<UserProfile> userProfiles = UserProfileConverter.ConvertToModelObject(UserProfileDAL.GetByKeys(null, userId));
            if (userProfiles.Count == 1)
                return (userProfiles[0]);
            return null;
        }

        public static List<User> GetUsersPaged(string userName, string orderBy, bool isAscending, int pageIndex, int pageSize)
        {
            List<User> users = UserConverter.ConvertToModelObject(UserDAL.GetUsersPaged(userName,orderBy,isAscending,pageIndex,pageSize));

            return users;
        }

        public static int GetUsersPagedCount(string _searchCustomerName)
        {
            return UserDAL.GetUsersPagedCount(_searchCustomerName);
        }
    }
}